
********************************************************************************
//Matieres suivies
cd $pathdata_processed
use tirage_temp, clear

keep npid fagkode
bysort npid (fagkode): gen j = _n

sum j
local max `r(max)'
reshape wide fagkode, i(npid) j(j)

gen matieres = fagkode1

foreach i of numlist 2/`max' {
	replace matieres = matieres + "," + fagkode`i' if !missing(fagkode`i')
}

keep npid matieres
egen matieres_num = group(matieres)
save matieres, replace
********************************************************************************	
//Instruments 
cd $pathdata_processed
use comb22005, clear

foreach year of numlist 2006/2009 {
	append using comb2`year'
	}

merge 1:1 npid fagkode using tirage_temp, nogen

gen chance = (predicted_gpa-expected_gpa)
gen chance_sd = (predicted_gpa-expected_gpa)/(variance_gpa)
replace chance_sd = chance if variance_gpa == 0

//grades
bysort npid: egen exam_grades = mean(real_skr)
bysort npid: egen oral_grades = mean(real_mun)

bysort npid: egen teachers_grades = mean(real_stp)
bysort npid: egen sd_teachers_grades = sd(real_stp)
replace sd_teachers_grades = 0 if nb_kurs == 1

bysort npid: egen mean_skr_hat = mean(skr_hat)
bysort npid: egen sd_skr_hat = sd(skr_hat)
replace sd_skr_hat = 0 if nb_kurs == 1

//kurs & grades
foreach i of numlist 1/1 {
	gen stp`i' = real_stp == `i'
	bysort npid: egen nb_course_`i' = sum(stp`i')
	}

keeporder npid nb_kurs nb_exam nb_oral teachers_grades sd_teachers_grades mean_skr_hat sd_skr_hat Tirage_final Fagkode expected_gpa predicted_gpa variance_gpa chance chance_sd exam_grades oral_grades nb_course_1 
duplicates drop

merge 1:1 npid using matieres, keep(1 3) nogen
save chance, replace
